{
  "name": "budzet domowy",
  "nodes": [
    {
      "parameters": {
        "jsCode": "const rawText = items[0].json.text || \"\";\n\n// Mapa do ujednolicenia nazw miesięcy\nconst monthMapFull = {\n    'sty': 'Styczeń', 'styczeń': 'Styczeń',\n    'lut': 'Luty', 'luty': 'Luty',\n    'mar': 'Marzec', 'marzec': 'Marzec',\n    'kwi': 'Kwiecień', 'kwiecień': 'Kwiecień',\n    'maj': 'Maj',\n    'cze': 'Czerwiec', 'czerwiec': 'Czerwiec',\n    'lip': 'Lipiec', 'lipiec': 'Lipiec',\n    'sie': 'Sierpień', 'sierpień': 'Sierpień',\n    'wrz': 'Wrzesień', 'wrzesień': 'Wrzesień',\n    'paź': 'Październik', 'październik': 'Październik',\n    'lis': 'Listopad', 'listopad': 'Listopad',\n    'gru': 'Grudzień', 'grudzień': 'Grudzień'\n};\n\n// Szukamy pierwszej daty: cyfry + słowo + rok (np. 25 luty 2026 lub 31 sty 2026)\nconst dateMatch = rawText.match(/(\\d{1,2})\\s+([a-z]+)\\s+(\\d{4})/i);\n\nlet nameSheet = \"Nieznany Arkusz\";\n\nif (dateMatch) {\n    const mKey = dateMatch[2].toLowerCase();\n    const miesiacNazwa = monthMapFull[mKey] || mKey;\n    const rok = dateMatch[3];\n    \n    // Tworzymy format: Miesiąc rok\n    nameSheet = `${miesiacNazwa} ${rok}`;\n}\n\n// Zwracamy tylko jeden obiekt z kluczem nameSheet\nreturn { nameSheet };"
      },
      "id": "06d70392-7b8a-48c8-a6f0-8b1efa99a616",
      "name": "Regex Parser2",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1680,
        768
      ]
    },
    {
      "parameters": {
        "path": "<REDACTED_FORM_PATH>",
        "formTitle": "n8n",
        "formFields": {
          "values": [
            {
              "fieldLabel": "plik",
              "fieldType": "file",
              "acceptFileTypes": ".pdf"
            }
          ]
        },
        "options": {}
      },
      "name": "n8n Form Trigger1",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 1,
      "position": [
        -2128,
        768
      ],
      "id": "4ea67418-621f-4d19-b28e-6fc5bed9d9fe",
      "webhookId": "<REDACTED_WEBHOOK_ID>"
    },
    {
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "=plik",
        "options": {}
      },
      "name": "Extract From File1",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        -1904,
        768
      ],
      "id": "444a7f01-a71e-4c7e-a146-b7961d67a095"
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "queryString": "={{ $json.nameSheet }}",
        "filter": {
          "includeTrashed": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -1456,
        768
      ],
      "id": "1be9e022-9f03-4747-9885-b6c588c42fe5",
      "name": "Search files and folders",
      "alwaysOutputData": true,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "<REDACTED_GOOGLE_DRIVE_CRED_ID>",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "aa3bf4e0-c1f1-432b-a1c7-da15b70d452e",
              "leftValue": "={{ $json.name }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -1232,
        768
      ],
      "id": "95db9d94-f722-4728-b6ba-ac0f3db76e40",
      "name": "If"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "4371a3b1-a961-4135-ba31-20a0c441b2f8",
              "leftValue": "={{ $json.Typ }}",
              "rightValue": "Wydatek",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -560,
        768
      ],
      "id": "01d23ccb-7e4e-4b26-8ceb-d7e36389a987",
      "name": "If1"
    },
    {
      "parameters": {
        "operation": "copy",
        "fileId": {
          "__rl": true,
          "value": "<REDACTED_FILE_ID>",
          "mode": "list",
          "cachedResultName": "Budżet miesięczny - wzór",
          "cachedResultUrl": "<REDACTED_DOC_URL>"
        },
        "name": "={{ $('Regex Parser2').item.json.nameSheet }}",
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -1008,
        848
      ],
      "id": "a5faf5fd-8ea2-47d0-bf36-d8002d305ea1",
      "name": "Copy file",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "<REDACTED_GOOGLE_DRIVE_CRED_ID>",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const rawText = $('Extract From File1').first().json.text || \"\";\n\nconst results = [];\n\n// Rozszerzona mapa miesięcy (obsługuje skróty i pełne nazwy z Twoich plików)\nconst monthMapDigits = {\n    'sty': '01', 'styczeń': '01',\n    'lut': '02', 'luty': '02',\n    'mar': '03', 'marzec': '03',\n    'kwi': '04', 'kwiecień': '04',\n    'maj': '05',\n    'cze': '06', 'czerwiec': '06',\n    'lip': '07', 'lipiec': '07',\n    'sie': '08', 'sierpień': '08',\n    'wrz': '09', 'wrzesień': '09',\n    'paź': '10', 'październik': '10',\n    'lis': '11', 'listopad': '11',\n    'gru': '12', 'grudzień': '12'\n};\n\n// Zmodyfikowany REGEX: [a-z]+ pozwala na różną długość nazwy miesiąca\nconst regex = /(\\d{1,2})\\s+([a-z]+)\\s+(\\d{4})\\s*\\nData księgowania\\s*\\n([\\s\\S]*?)\\n(-?\\d+[.,]?\\d*)\\s*PLN/gi;\n\nlet match;\nwhile ((match = regex.exec(rawText)) !== null) {\n    const day = match[1].padStart(2, '0');\n    const monthKey = match[2].toLowerCase();\n    const month = monthMapDigits[monthKey] || '00';\n    const year = match[3];\n    \n    const kwotaRaw = match[5].replace(',', '.');\n    const kwotaNumer = parseFloat(kwotaRaw);\n\n    results.push({\n        Data: `${day}.${month}.${year}`,\n        Opis: match[4].trim().replace(/\\n/g, ' '), // Czyszczenie opisu z enterów\n        Kwota: Math.abs(kwotaNumer).toFixed(2).replace('.', ','),\n        Typ: kwotaNumer < 0 ? 'Wydatek' : 'Wpływ',\n        // Nowa logika kategorii\n        Kategoria: kwotaNumer < 0 ? 'Jedzenie' : 'Pensja'\n    });\n}\n\nreturn results;"
      },
      "id": "f5f62055-6736-42e6-afab-d34169ca329e",
      "name": "Regex Parser",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -784,
        768
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "={{ $('Search files and folders').item.json.id || $('Copy file').item.json.id }}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": 1732160294,
          "mode": "list",
          "cachedResultName": "Transakcje",
          "cachedResultUrl": "<REDACTED_DOC_URL_2>"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Data": "={{ $json.Data }}",
            "Opis": "={{ $json.Opis }}",
            "Kwota": "={{ $json.Kwota }}",
            "Kategoria": "={{ $json.Kategoria }}"
          },
          "matchingColumns": [
            "Aby zmienić lub dodać kategorie, zaktualizuj tabele Wydatki i Przychody w arkuszu Podsumowanie."
          ],
          "schema": [
            {
              "id": "Data",
              "displayName": "Data",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Kwota",
              "displayName": "Kwota",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Opis",
              "displayName": "Opis",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Kategoria",
              "displayName": "Kategoria",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "P-Data",
              "displayName": "P-Data",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "P-Kwota",
              "displayName": "P-Kwota",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "P-Opis",
              "displayName": "P-Opis",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "P-Kategoria",
              "displayName": "P-Kategoria",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "locationDefine": {
            "values": {
              "headerRow": 4
            }
          }
        }
      },
      "id": "3747fe7a-001e-48b0-a052-ae4e0760df3c",
      "name": "Zapisz wydatki",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        -336,
        672
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "<REDACTED_GOOGLE_SHEETS_CRED_ID>",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "={{ $('Search files and folders').item.json.id || $('Copy file').item.json.id  }}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": 1732160294,
          "mode": "list",
          "cachedResultName": "Transakcje",
          "cachedResultUrl": "<REDACTED_DOC_URL_3>"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "P-Data": "={{ $json.Data }}",
            "P-Opis": "={{ $json.Opis }}",
            "P-Kwota": "={{ $json.Kwota }}",
            "P-Kategoria": "={{ $json.Kategoria }}"
          },
          "matchingColumns": [
            "Aby zmienić lub dodać kategorie, zaktualizuj tabele Wydatki i Przychody w arkuszu Podsumowanie."
          ],
          "schema": [
            {
              "id": "Data",
              "displayName": "Data",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Kwota",
              "displayName": "Kwota",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Opis",
              "displayName": "Opis",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Kategoria",
              "displayName": "Kategoria",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "P-Data",
              "displayName": "P-Data",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "P-Kwota",
              "displayName": "P-Kwota",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "P-Opis",
              "displayName": "P-Opis",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "P-Kategoria",
              "displayName": "P-Kategoria",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "locationDefine": {
            "values": {
              "headerRow": 4
            }
          }
        }
      },
      "id": "04e34c2b-4577-40af-a566-e8335dc7ee0e",
      "name": "Zapisz przychody",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        -336,
        864
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "<REDACTED_GOOGLE_SHEETS_CRED_ID>",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Regex Parser2": {
      "main": [
        [
          {
            "node": "Search files and folders",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "n8n Form Trigger1": {
      "main": [
        [
          {
            "node": "Extract From File1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract From File1": {
      "main": [
        [
          {
            "node": "Regex Parser2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search files and folders": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Regex Parser",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Copy file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If1": {
      "main": [
        [
          {
            "node": "Zapisz wydatki",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Zapisz przychody",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Copy file": {
      "main": [
        [
          {
            "node": "Regex Parser",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Regex Parser": {
      "main": [
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "availableInMCP": false
  },
  "versionId": "83558e50-90d3-4b14-b2ec-0c05bb6b9edc",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "<REDACTED_INSTANCE_ID>"
  },
  "id": "l6sM80jvEoi3m3SU",
  "tags": []
}